﻿using MaterialDesignThemes.Wpf;
using Microsoft.EntityFrameworkCore.Query.Internal;
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Timers;
using System.Windows;
using WpfApp1.Commands;
using WpfApp1.Contexts;
using WpfApp1.Models;
using WpfApp1.Services;

namespace WpfApp1.ViewModels
{
    public class MainWindowViewModel:NotificationObject
    {
        private string name;
        public string Name{ 
            get { return name; }
            set { this.name = value; }
        }
        private string email;
        public string Email
        {
            get { return email; }
            set { this.email = value; }
        }
        private string password;
        public string Password
        {
            get { return password; }
            set { this.password = value; }
        }
        private string repeatPassword;
        public string RepeatPassword
        {
            get { return repeatPassword; }
            set { this.repeatPassword = value; }
        }
        private string content;

        public event NotifyCollectionChangedEventHandler? CollectionChanged;

        public string Content
        {
            get { return content; }
            set { this.content=value;
                this.RaisPropertyChange("Content");
            }
        }
        private bool active;
        public bool Active
        {
            get { return active; }
            set { this.active = value;
                this.RaisPropertyChange("Active");
            }
        }
        public DelegateCommand LoginCommand { get; set; }
        
        private void Login(object parameter)
        {
            if (this.name == null || this.email == null || this.password == null || this.repeatPassword == null)
            {
                this.Content = "请填写所有信息";
                this.Active = true;
                return;
            }

            User user = new User();
            user.UserName = this.name;
            user.Email = this.email;
            user.Password = this.password;
            if(user.Password != this.RepeatPassword)
            {
                this.Content = "重复密码与密码不一致";
                this.Active = true;
                return;
            }
            IUserService iu = new UserServiceImpl();
            var u = iu.getUser(Name);
            if (u != null)
            {
                if (u.Equals(user))
                {
                    this.Content = "登录成功";
                    this.Active = true;
                }
                else
                {
                    this.Content = "用户名或者密码错误";
                    this.Active = true;
                }
            }
        
        }
        public MainWindowViewModel()
        {
            this.LoginCommand = new DelegateCommand();
            this.LoginCommand.ExecuteAction = new Action<object>(this.Login);
        }
    }
}
